package com.facebank.usersupport.service.impl;

import com.facebank.usersupport.dto.AgentAssDto;
import com.facebank.usersupport.dto.ProfitDto;
import com.facebank.usersupport.mapper.usersupport.usersupport.AssAgentMapper;
import com.facebank.usersupport.mapper.usersupport.usersupport.AssociatorMapper;
import com.facebank.usersupport.model.AssAgent;
import com.facebank.usersupport.model.Associator;
import com.facebank.usersupport.model.PageRestModel;
import com.facebank.usersupport.model.RestModel;
import com.facebank.usersupport.service.IAgentService;
import com.facebank.usersupport.util.SessionUtil;
import com.facebank.usersupport.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: yaozun
 * @Date: 2018/4/3 0003 09:26
 * @Description:
 */
@Service
public class AgentService implements IAgentService {
    @Autowired
    private AssociatorMapper associatorMapper;
    @Autowired
    private AssAgentMapper assAgentMapper;


    @Override
    public RestModel add(Associator associator, HttpSession session) {
        Boolean hasPhone = checkPhone(associator.getPhone());
        if (hasPhone) {
            return new RestModel("手机号已被注册");
        }
        associator.setCreateTime(System.currentTimeMillis());
        associator.setPassword("dgut123");
        associatorMapper.insertSelective(associator);
        AssAgent assAgent = new AssAgent();
        assAgent.setUserId(Long.valueOf(SessionUtil.getUserId(session)));
        assAgent.setAssociatorId(associator.getAssociatorId());
        assAgent.setCreateTime(System.currentTimeMillis());
        assAgent.setUpdateTime(System.currentTimeMillis());
        assAgentMapper.insertSelective(assAgent);
        return new RestModel("添加成功");
    }

    @Override
    public RestModel list(Integer pageNo, Integer length, String associatorName, String phone, Short status, Long startTime, Long endTime, HttpSession session) {
        status = (short) StrUtil.parseStringToInt(status, -1);
        PageHelper.startPage(pageNo, length);
        Long userId = Long.valueOf(SessionUtil.getUserId(session));
        List<AgentAssDto> agentAssDtos = associatorMapper.selectAgentAss(phone, startTime, endTime, userId);
        PageInfo<AgentAssDto> pageInfo = new PageInfo<>(agentAssDtos);
        return new RestModel(pageInfo);
    }

    @Override
    public RestModel profit(String phone, Long startTime, Long endTime, HttpSession session, int pageNo, int length, String draw) {
        PageHelper.startPage(pageNo, length);
        Long userId = Long.valueOf(SessionUtil.getUserId(session));
        List<ProfitDto> profitDtos = assAgentMapper.selectProfit(phone, startTime, endTime, userId);
        PageInfo<ProfitDto> pageInfo = new PageInfo<>(profitDtos);

        PageRestModel pageRestModel = new PageRestModel(
                draw,
                pageInfo.getTotal(),
                pageInfo.getTotal(),
                pageInfo.getList()
        );

        return new RestModel(pageRestModel);
    }

    //判断手机号是否存在
    public boolean checkPhone(String phone) {
        Integer count = associatorMapper.checkPhone(phone);
        if (count == 0) {
            return false;
        } else {
            return true;
        }
    }
}
